package com.yihu.wlyy.job;

import com.yihu.wlyy.entity.*;
import com.yihu.wlyy.entity.address.Hospital;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.entity.consult.ConsultTeam;
import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.doctor.DoctorPatientGroupInfo;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.PatientHealthGuidance;
import com.yihu.wlyy.repository.*;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.IdCardUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016.08.23.
 * 实时统计今天的数据
 */
@Component
public class CurrentDayAllQuotaJob implements Job {
    @Autowired
    private SignFamilyDao signFamilyDao;
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private TownDao townDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ConsultTeamDao consultTeamDao;
    @Autowired
    private PatientHealthGuidanceDao patientHealthGuidanceDao;
    @Autowired
    private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;
    @Autowired
    private PatientDao patientDao;
    @Autowired
    private WlyyJobLogDao wlyyJobLogDao;//执行日志Dao

    private org.apache.tomcat.jdbc.pool.DataSource fvdataSource = new org.apache.tomcat.jdbc.pool.DataSource();//随访数据源

    private JdbcTemplate jdbcTemplate = new JdbcTemplate();

    private Properties systemProperties;
    List<Town> towns = null;//厦门市全部的区
    List<Hospital> hospitals = null;//系统全部的机构
    List<Doctor> qkdoctors = null;//系统全部的全科医生
    List<Doctor> jkglsdoctors = null;//系统全部的健康管理师
    Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
    Map<String, Doctor> jkglsdoctorsMap = new HashMap<String, Doctor>();
    Map<String, Doctor> qkdoctorsMap = new HashMap<String, Doctor>();
    Map<String, Town> townsMap = new HashMap<String, Town>();
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {

        try {
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            getSystemProperties();
            fvdataSource.setDriverClassName(systemProperties.get("fv.jdbc.driver").toString());
            fvdataSource.setUrl(systemProperties.get("fv.jdbc.url").toString());
            fvdataSource.setUsername(systemProperties.get("fv.jdbc.username").toString());
            fvdataSource.setPassword(systemProperties.get("fv.jdbc.password").toString());

            jdbcTemplate.setDataSource(fvdataSource);//初始化随访的数据源
            towns = townDao.findByCityCode(Constant.city); //查找出厦门市全部的区
            hospitals = hospitalDao.findHospitalzxFWZ(); //查找出系统全部的机构
            for (Hospital hospital : hospitals) {
                hospitalsMap.put(hospital.getCode(), hospital);
            }
            qkdoctors = doctorDao.findAllQKDoctot();//查找出系统全部的全科医生
            for (Doctor doctor : qkdoctors) {
                qkdoctorsMap.put(doctor.getCode(), doctor);
            }
            jkglsdoctors = doctorDao.findAllZKDoctot();//查找出系统全部的全科医生
            for (Doctor doctor : jkglsdoctors) {
                jkglsdoctorsMap.put(doctor.getCode(), doctor);
            }
            for (Town town : towns) {
                townsMap.put(town.getCode(), town);
            }
            //计算指标
            computequota();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //统计
    private void computequota() throws Exception {
        WlyyJobLog wlyyJobLog=new WlyyJobLog();
        wlyyJobLog.setJobStartTime(new Date());


        computequota_1(); //统计今天的签约
        computequota_2(); //统计今天的解约
        computequota_3(); //统计今天的健康咨询量
        computequota_4(); //统计今天的随访量
        computequota_5(); //统计今天的健康指导
        computequota_6(); //统计今天的签约患者性别
        computequota_7();//统计今天的签约患者分组
        computequota_8();//统计今天的签约患者年龄
        computequota_9();//统计今天的待签约
        computequota_10();//统计今天的改签
        wlyyJobLog.setJobContent("开始实时统计查询");
        wlyyJobLog.setJobName("实时统计");
        wlyyJobLog.setJobId("11");
        wlyyJobLog.setJobEndTime(new Date());
        wlyyJobLogDao.save(wlyyJobLog);
    }

    private void computequota_1() {
        //签约需要的统计map
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjQkdoctorMap = new HashMap<String, JSONObject>();//团队级的统计map 目前没有团队 先用全科医生统一  key doctorCode

        initMap(tjCityMap, tjTownMap, tjOrgMap, tjQkdoctorMap, null);

        //找出今天的签约信息 yesterday,now
        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(now, tomorrow);

        //统计有签约的
        for (SignFamily signFamily : signFamilys) {
            String hospitalCode=signFamily.getHospital();
            Hospital hospital=hospitalsMap.get(hospitalCode);
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(hospital.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            //判断是中心还是服务站 服务站的话添加到中心去
            if(!"00".equals(hospital.getCode().substring(8))){
                String orgCodeTemp=hospital.getCode().substring(0,8)+"00";
                JSONObject  orgjo=tjOrgMap.get(orgCodeTemp);
                orgjo.put("num",((int)orgjo.get("num"))+1);
            }else{
                JSONObject orgjo=tjOrgMap.get(hospital.getCode());
                orgjo.put("num",((int)orgjo.get("num"))+1);
            }
            //统计全科医生
            JSONObject qkjo=tjQkdoctorMap.get(signFamily.getDoctor());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //数据保存到redis
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:1:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:1:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:1:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjQkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:1:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    private void initMap(Map<String, JSONObject> tjCityMap, Map<String, JSONObject> tjTownMap, Map<String, JSONObject> tjOrgMap, Map<String, JSONObject> tjQkdoctorMap, Map<String, JSONObject> tjJKdoctorMap) {
        JSONObject jo=new JSONObject();
        jo.put("name", Constant.cityName);
        jo.put("num",0);
        tjCityMap.put(Constant.city,jo);
        for(Town town:towns){
            jo=new JSONObject();
            jo.put("name",town.getName());
            jo.put("num",0);
            jo.put("date", DateUtil.dateToStrLong(new Date()));
            tjTownMap.put(town.getCode(),jo);
        }
        for(Hospital hospital:hospitals){
            jo=new JSONObject();
            jo.put("name",hospital.getName());
            jo.put("num",0);
            jo.put("date", DateUtil.dateToStrLong(new Date()));
            tjOrgMap.put(hospital.getCode(),jo);
        }
        if (tjQkdoctorMap!=null){
            for(Doctor doctor:qkdoctors){
                jo=new JSONObject();
                jo.put("name",doctor.getName());
                jo.put("num",0);
                jo.put("date", DateUtil.dateToStrLong(new Date()));
                tjQkdoctorMap.put(doctor.getCode(),jo);
            }
        }
        if(tjJKdoctorMap!=null){
            for(Doctor doctor:jkglsdoctors){
                jo=new JSONObject();
                jo.put("name",doctor.getName());
                jo.put("num",0);
                jo.put("date", DateUtil.dateToStrLong(new Date()));
                tjJKdoctorMap.put(doctor.getCode(),jo);
            }
        }
    }

    private void computequota_2() {

        //签约需要的统计map
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjQkdoctorMap = new HashMap<String, JSONObject>();//团队级的统计map 目前没有团队 先用全科医生统一  key doctorCode

        initMap(tjCityMap, tjTownMap, tjOrgMap, tjQkdoctorMap, null);


        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingUnSignYesterday(now, tomorrow);

        //统计有签约的
        for (SignFamily signFamily : signFamilys) {
            String hospitalCode=signFamily.getHospital();
            Hospital hospital=hospitalsMap.get(hospitalCode);
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(hospital.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            //判断是中心还是服务站 服务站的话添加到中心去
            if(!"00".equals(hospital.getCode().substring(8))){
                String orgCodeTemp=hospital.getCode().substring(0,8)+"00";
                JSONObject  orgjo=tjOrgMap.get(orgCodeTemp);
                orgjo.put("num",((int)orgjo.get("num"))+1);
            }else{
                JSONObject orgjo=tjOrgMap.get(hospital.getCode());
                orgjo.put("num",((int)orgjo.get("num"))+1);
            }
            //统计全科医生
            JSONObject qkjo=tjQkdoctorMap.get(signFamily.getDoctor());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //数据保存到redis
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:2:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:2:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:2:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjQkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:2:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    private void computequota_3() {
        //签约需要的统计map
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjJkdoctorMap = new HashMap<String, JSONObject>();//全部的健康管理师

        initMap(tjCityMap, tjTownMap, tjOrgMap, null, tjJkdoctorMap);

        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<ConsultTeam> consultTeams = consultTeamDao.findByCzrqyYesterday(now,tomorrow);
        //统计
        for (ConsultTeam consultTeam : consultTeams) {
            String doctorCode=consultTeam.getDoctor();
            Doctor doctor=jkglsdoctorsMap.get(doctorCode);//得到健康管理师
            if(doctor==null){
                continue;
            }
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(doctor.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            JSONObject orgjo=tjOrgMap.get(doctor.getHospital());
            orgjo.put("num",((int)orgjo.get("num"))+1);
            //统计全科医生
            JSONObject qkjo=tjJkdoctorMap.get(doctor.getCode());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //数据保存到redis
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:3:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:3:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:3:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjJkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:3:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    private void computequota_4() {
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjJkdoctorMap = new HashMap<String, JSONObject>();//全部的健康管理师

        initMap(tjCityMap, tjTownMap, tjOrgMap, null, tjJkdoctorMap);
        //找出今天的随访信息
        List<Map<String,Object>> plans = getFvYesterday();
        //统计有随访的
        for (Map<String,Object> plan : plans) {
            String doctorCode = (String) plan.get("doctor_uid");//得到随访中健康医生的code
            Doctor doctor=jkglsdoctorsMap.get(doctorCode);//得到健康管理师
            if(doctor==null){
                continue;
            }
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(doctor.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            JSONObject orgjo=tjOrgMap.get(doctor.getHospital());
            orgjo.put("num",((int)orgjo.get("num"))+1);
            //统计全科医生
            JSONObject qkjo=tjJkdoctorMap.get(doctor.getCode());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:4:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:4:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:4:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjJkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:4:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    private void computequota_5() {
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjQkdoctorMap = new HashMap<String, JSONObject>();//团队级的统计map 目前没有团队 先用全科医生统一  key doctorCode
        Map<String, JSONObject> tjJkdoctorMap = new HashMap<String, JSONObject>();//全部的健康管理师

        initMap(tjCityMap, tjTownMap, tjOrgMap, tjQkdoctorMap, tjJkdoctorMap);
        //找出今天的健康指导信息

        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<PatientHealthGuidance> patientHealthGuidances = patientHealthGuidanceDao.findByCzrqyYesterday(now,tomorrow);
        for (PatientHealthGuidance patientHealthGuidance: patientHealthGuidances) {
            String doctorCode =patientHealthGuidance.getDoctor();
            Doctor doctor=jkglsdoctorsMap.get(doctorCode);//得到健康管理师
            if(doctor==null){
                doctor=qkdoctorsMap.get(doctorCode);//得到全科医生
                if(doctor==null){
                    continue;
                }
            }
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(doctor.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            JSONObject orgjo=tjOrgMap.get(doctor.getHospital());
            orgjo.put("num",((int)orgjo.get("num"))+1);
            //统计全科医生
            JSONObject qkjo=tjQkdoctorMap.get(doctor.getCode());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:5:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:5:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:5:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjQkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:5:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    private void computequota_6() throws Exception {
        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(now, tomorrow);
        //统计有签约的
        Map<String, Map<String, Long>> citySexMap = new HashMap<String, Map<String, Long>>();//key是市行政代码 目前只有厦门市
        citySexMap.put(Constant.city,new HashMap<String, Long>());
        Map<String, Map<String, Long>> townSexMap = new HashMap<String, Map<String, Long>>();//key是区行政代码
        Map<String, Map<String, Long>> orgSexMap = new HashMap<String, Map<String, Long>>();//key是机构代码
        for (SignFamily signFamily : signFamilys) {
            String idcard = signFamily.getIdcard();//得到签约中患者的身份证号
            Hospital hospital = hospitalsMap.get(signFamily.getHospital());//得到患者签约的机构
            if(hospital==null){
                continue;
            }
            String town = hospital.getTown();//得到患者签约的机构的区号
            String hospitalCode = hospital.getCode();//得到患者签约的机构号
            String sex = IdCardUtil.getSexForIdcard(idcard);//根据身份证得到性别
            //统计市 目前只统计厦门市 所以先写死 后期再改造
            if (citySexMap.containsKey(Constant.city)) {
                Map<String, Long> sexMap = citySexMap.get(Constant.city);
                sexMap.put(sex, (sexMap.get(sex)==null?0l:sexMap.get(sex)) + 1);
                citySexMap.put(Constant.city, sexMap);
            } else {
                Map<String, Long> sexMap = new HashMap<String, Long>();
                sexMap.put(sex, 1L);
                citySexMap.put(Constant.city, sexMap);
            }
            //统计区
            if (townSexMap.containsKey(town)) {
                Map<String, Long> sexMap = townSexMap.get(town);
                sexMap.put(sex, (sexMap.get(sex)==null?0l:sexMap.get(sex)) + 1);
                townSexMap.put(town, sexMap);
            } else {
                Map<String, Long> sexMap = new HashMap<String, Long>();
                sexMap.put(sex, 1L);
                townSexMap.put(town, sexMap);
            }
            //统计机构
            if (orgSexMap.containsKey(hospitalCode)) {
                Map<String, Long> sexMap = orgSexMap.get(hospitalCode);
                sexMap.put(sex, (sexMap.get(sex)==null?0l:sexMap.get(sex)) + 1);
                orgSexMap.put(hospitalCode, sexMap);
            } else {
                Map<String, Long> sexMap = new HashMap<String, Long>();
                sexMap.put(sex, 1L);
                orgSexMap.put(hospitalCode, sexMap);
            }
        }
        //保存统计数据
        //保存市统计数据
        for (Map.Entry<String, Map<String, Long>> entry : citySexMap.entrySet()) {
            //保存女
            JSONObject jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_sex_1)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_sex_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:4:1:"+entry.getKey()).toString(),jsonObject.toString());
            //保存男
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_sex_2)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_sex_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:4:2:"+entry.getKey()).toString(),jsonObject.toString());
            //保存未知
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_sex_3)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_sex_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:4:3:"+entry.getKey()).toString(),jsonObject.toString());
        }
        //保存区的统计数据
        for (Map.Entry<String, Town> townEntry : townsMap.entrySet()) {
            //判断该区是否有统计数据
            Map<String, Long> oneTownSexMap = townSexMap.get(townEntry.getKey());//得到当个区的统计数据
            Town town = townEntry.getValue();//得到区级信息
            //保存女
            JSONObject jsonObject=new JSONObject();
            if (oneTownSexMap != null && oneTownSexMap.containsKey(Constant.level_sex_1)) {
                jsonObject.put("num",oneTownSexMap.get(Constant.level_sex_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:3:1:"+townEntry.getKey()).toString(),jsonObject.toString());
            //保存男
            jsonObject=new JSONObject();
            if (oneTownSexMap != null && oneTownSexMap.containsKey(Constant.level_sex_2)) {
                jsonObject.put("num",oneTownSexMap.get(Constant.level_sex_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:3:2:"+townEntry.getKey()).toString(),jsonObject.toString());
            //保存未知
            jsonObject=new JSONObject();
            if (oneTownSexMap != null && oneTownSexMap.containsKey(Constant.level_sex_3)) {
                jsonObject.put("num",oneTownSexMap.get(Constant.level_sex_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:3:3:"+townEntry.getKey()).toString(),jsonObject.toString());
        }
        //统计机构
        for (Map.Entry<String, Hospital> hospitalEntry : hospitalsMap.entrySet()) {
            //判断该区是否有统计数据
            Map<String, Long> oneTownSexMap = orgSexMap.get(hospitalEntry.getKey());//得到当个区的统计数据
            Hospital town = hospitalEntry.getValue();//得到区级信息
            //保存女
            JSONObject jsonObject=new JSONObject();
            if (oneTownSexMap != null && oneTownSexMap.containsKey(Constant.level_sex_1)) {
                jsonObject.put("num",oneTownSexMap.get(Constant.level_sex_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:2:1:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
            //保存男
            jsonObject=new JSONObject();
            if (oneTownSexMap != null && oneTownSexMap.containsKey(Constant.level_sex_2)) {
                jsonObject.put("num",oneTownSexMap.get(Constant.level_sex_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:2:2:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
            //保存未知
            jsonObject=new JSONObject();
            if (oneTownSexMap != null && oneTownSexMap.containsKey(Constant.level_sex_3)) {
                jsonObject.put("num",oneTownSexMap.get(Constant.level_sex_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:6:2:3:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
        }

    }

    private void computequota_7() {

        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(now, tomorrow);
        //查找出系统全部的患者
        List<Patient> patients = patientDao.findAllSignPatient();
        Map<String, Patient> patientsMap = new HashMap<String, Patient>();
        for (Patient patient : patients) {
            patientsMap.put(patient.getCode(), patient);
        }
        Map<String, Map<String, Long>> cityGroupMap = new HashMap<String, Map<String, Long>>();//key是市行政代码 目前只有厦门市
        cityGroupMap.put(Constant.city,new HashMap<String, Long>());
        Map<String, Map<String, Long>> townGroupMap = new HashMap<String, Map<String, Long>>();//key是区行政代码
        Map<String, Map<String, Long>> orgGroupMap = new HashMap<String, Map<String, Long>>();//key是机构代码

        //得到所有的签约病人的分组信息
        List<DoctorPatientGroupInfo> doctorPatientGroupInfos = doctorPatientGroupInfoDao.findAllPatient();
        Map<String, String> groupMap = new HashMap<String, String>();
        for (DoctorPatientGroupInfo doctorPatientGroupInfo : doctorPatientGroupInfos) {
            groupMap.put(doctorPatientGroupInfo.getPatient(), doctorPatientGroupInfo.getGroup());
        }

        //统计有签约的
        for (SignFamily signFamily : signFamilys) {
            String patient=signFamily.getPatient();
            String group = groupMap.get(patient);//得到该签约病人的所在分组 1 普通 2 慢病 3是 65岁以上人群
            Hospital hospital = hospitalsMap.get(signFamily.getHospital());//得到患者签约的机构
            if(hospital==null){
                continue;
            }
            String town = hospital.getTown();
            //统计市  现在只有厦门市 默认先写死 后面再改
            if (cityGroupMap.containsKey(Constant.city)) {
                Map<String, Long> groupMapTemp = cityGroupMap.get(Constant.city);
                groupMapTemp.put(group,(groupMapTemp.get(group)==null?0l:groupMapTemp.get(group))  + 1);
                cityGroupMap.put(Constant.city, groupMapTemp);
            } else {
                Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                groupMapTemp.put(group, 1L);
                cityGroupMap.put(Constant.city, groupMapTemp);
            }
            //统计区
            if (townGroupMap.containsKey(town)) {
                Map<String, Long> groupMapTemp = townGroupMap.get(town);
                groupMapTemp.put(group, (groupMapTemp.get(group)==null?0l:groupMapTemp.get(group))+ 1);
                townGroupMap.put(town, groupMapTemp);
            } else {
                Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                groupMapTemp.put(group, 1L);
                townGroupMap.put(town, groupMapTemp);
            }
            //统计机构
            if (orgGroupMap.containsKey(hospital.getCode())) {
                Map<String, Long> groupMapTemp = orgGroupMap.get(hospital.getCode());
                groupMapTemp.put(group, (groupMapTemp.get(group)==null?0l:groupMapTemp.get(group)) + 1);
                orgGroupMap.put(hospital.getCode(), groupMapTemp);
            } else {
                Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                groupMapTemp.put(group, 1L);
                orgGroupMap.put(hospital.getCode(), groupMapTemp);
            }
            boolean hasGXY = false;//有高血压
            boolean hasTNB = false;//有糖尿病
            if ("2".equals(group)) {
                //如果是慢病的 统计高血压的 糖尿病  1高血压，2糖尿病
                String jsonString = redisTemplate.opsForValue().get("disease:" + patient);
                if (StringUtils.isEmpty(jsonString)) {
                    continue;
                }
                //排除数据 只留下高血压和糖尿病
                List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
                JSONArray redisValues = new JSONArray(jsonString);
                for (Object obj : redisValues) {
                    JSONObject redisValue = new JSONObject(obj);
                    //排除掉三师签约
                    if ("1".equals(redisValue.get("signType").toString())) {
                        continue;
                    }
                    String disease = redisValue.get("disease").toString();
                    if (Integer.valueOf(disease).equals("1")) {
                        jsonObjects.add(redisValue);
                        hasGXY = true;//设置有高血压
                    }
                    if (Integer.valueOf(disease).equals("2")) {
                        jsonObjects.add(redisValue);
                        hasTNB = true;//设置有糖尿病
                    }
                }
                if(jsonObjects.size()==0){
                    continue;//如果没有高血压和糖尿病 不统计
                }
                if (hasGXY && hasTNB) {
                    group = "100";//有高血压又有糖尿病
                } else {
                    group = Integer.valueOf(jsonObjects.get(0).get("disease").toString()) + 3 + "";//4高血压，5糖尿病
                }
                if (cityGroupMap.containsKey(Constant.city)) {
                    Map<String, Long> groupMapTemp = cityGroupMap.get(Constant.city);
                    groupMapTemp.put(group, (groupMapTemp.get(group) == null ? 0l : groupMapTemp.get(group)) + 1);
                    cityGroupMap.put(Constant.city, groupMapTemp);
                } else {
                    Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                    groupMapTemp.put(group, 1L);
                    cityGroupMap.put(Constant.city, groupMapTemp);
                }
                //统计区
                if (townGroupMap.containsKey(town)) {
                    Map<String, Long> groupMapTemp = townGroupMap.get(town);
                    groupMapTemp.put(group, (groupMapTemp.get(group) == null ? 0l : groupMapTemp.get(group)) + 1);
                    townGroupMap.put(town, groupMapTemp);
                } else {
                    Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                    groupMapTemp.put(group, 1L);
                    townGroupMap.put(town, groupMapTemp);
                }
                //统计机构
                //统计站
                if (!"00".equals(hospital.getCode().substring(8))) {
                    String orgCodeTemp = hospital.getCode().substring(0, 8) + "00";
                    //统计机构
                    if (orgGroupMap.containsKey(orgCodeTemp)) {
                        Map<String, Long> groupMapTemp = orgGroupMap.get(orgCodeTemp);
                        groupMapTemp.put(group, (groupMapTemp.get(group) == null ? 0l : groupMapTemp.get(group)) + 1);
                        orgGroupMap.put(orgCodeTemp, groupMapTemp);
                    } else {
                        Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                        groupMapTemp.put(group, 1L);
                        orgGroupMap.put(orgCodeTemp, groupMapTemp);
                    }
                } else {
                    //统计机构
                    if (orgGroupMap.containsKey(hospital.getCode())) {
                        Map<String, Long> groupMapTemp = orgGroupMap.get(hospital.getCode());
                        groupMapTemp.put(group, (groupMapTemp.get(group) == null ? 0l : groupMapTemp.get(group)) + 1);
                        orgGroupMap.put(hospital.getCode(), groupMapTemp);
                    } else {
                        Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                        groupMapTemp.put(group, 1L);
                        orgGroupMap.put(hospital.getCode(), groupMapTemp);
                    }
                }

            }
        }
        //保存统计数据
        //保存市的统计数据
        for (Map.Entry<String, Map<String, Long>> entry : cityGroupMap.entrySet()) {
            //保存健康人群
            JSONObject jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_group_1)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_group_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:4:1:"+entry.getKey()).toString(),jsonObject.toString());

            //保存慢病人群
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_group_2)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_group_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:4:2:"+entry.getKey()).toString(),jsonObject.toString());

            //保存65岁以上人群
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_group_3)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_group_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:4:3:"+entry.getKey()).toString(),jsonObject.toString());
            //保存高血压
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_group_4)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_group_4));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:4:4:"+entry.getKey()).toString(),jsonObject.toString());
            //保存糖尿病
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_group_5)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_group_5));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:4:5:"+entry.getKey()).toString(),jsonObject.toString());

            //保存糖尿病和高血压
            jsonObject=new JSONObject();
            if (entry.getValue() != null && entry.getValue().containsKey(Constant.level_group_100)) {
                jsonObject.put("num",entry.getValue().get(Constant.level_group_100));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:4:100:"+entry.getKey()).toString(),jsonObject.toString());
        }

        //保存区级
        //保存区的统计数据
        for (Map.Entry<String, Town> townEntry : townsMap.entrySet()) {//保存健康人群
            JSONObject jsonObject=new JSONObject();
            Town town=townsMap.get(townEntry.getKey());
            Map<String, Long> oneTown= townGroupMap.get(townEntry.getKey());
            if (oneTown != null && oneTown.containsKey(Constant.level_group_1)) {
                jsonObject.put("num",oneTown.get(Constant.level_group_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:3:1:"+townEntry.getKey()).toString(),jsonObject.toString());

            //保存慢病人群
            jsonObject=new JSONObject();
            if (oneTown != null && oneTown.containsKey(Constant.level_group_2)) {
                jsonObject.put("num",oneTown.get(Constant.level_group_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:3:2:"+townEntry.getKey()).toString(),jsonObject.toString());

            //保存65岁以上人群
            jsonObject=new JSONObject();
            if (oneTown != null && oneTown.containsKey(Constant.level_group_3)) {
                jsonObject.put("num",oneTown.get(Constant.level_group_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:3:3:"+townEntry.getKey()).toString(),jsonObject.toString());
            //保存高血压
            jsonObject=new JSONObject();
            if (oneTown != null && oneTown.containsKey(Constant.level_group_4)) {
                jsonObject.put("num",oneTown.get(Constant.level_group_4));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:3:4:"+townEntry.getKey()).toString(),jsonObject.toString());
            //保存糖尿病
            jsonObject=new JSONObject();
            if (oneTown != null && oneTown.containsKey(Constant.level_group_5)) {
                jsonObject.put("num",oneTown.get(Constant.level_group_5));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:3:5:"+townEntry.getKey()).toString(),jsonObject.toString());
            //保存糖尿病和高血压
            jsonObject=new JSONObject();
            if (oneTown != null && oneTown.containsKey(Constant.level_group_100)) {
                jsonObject.put("num",oneTown.get(Constant.level_group_100));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:3:100:"+townEntry.getKey()).toString(),jsonObject.toString());
        }
        //保存机构
        for (Map.Entry<String, Hospital> hospitalEntry : hospitalsMap.entrySet()) {
            //保存健康人群
            Hospital hospital=hospitalsMap.get(hospitalEntry.getKey());
            JSONObject jsonObject=new JSONObject();

            Map<String, Long> oneOrg= orgGroupMap.get(hospitalEntry.getKey());
            if (oneOrg != null && oneOrg.containsKey(Constant.level_group_1)) {
                jsonObject.put("num",oneOrg.get(Constant.level_group_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:2:1:"+hospitalEntry.getKey()).toString(),jsonObject.toString());

            //保存慢病人群
            jsonObject=new JSONObject();
            if (oneOrg != null && oneOrg.containsKey(Constant.level_group_2)) {
                jsonObject.put("num",oneOrg.get(Constant.level_group_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:2:2:"+hospitalEntry.getKey()).toString(),jsonObject.toString());

            //保存65岁以上人群
            jsonObject=new JSONObject();
            if (oneOrg != null && oneOrg.containsKey(Constant.level_group_3)) {
                jsonObject.put("num",oneOrg.get(Constant.level_group_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:2:3:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
            //保存高血压
            jsonObject=new JSONObject();
            if (oneOrg != null && oneOrg.containsKey(Constant.level_group_4)) {
                jsonObject.put("num",oneOrg.get(Constant.level_group_4));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:2:4:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
            //保存糖尿病
            jsonObject=new JSONObject();
            if (oneOrg != null && oneOrg.containsKey(Constant.level_group_5)) {
                jsonObject.put("num",oneOrg.get(Constant.level_group_5));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:2:5:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
            //保存糖尿病和高血压
            jsonObject=new JSONObject();
            if (oneOrg != null && oneOrg.containsKey(Constant.level_group_100)) {
                jsonObject.put("num",oneOrg.get(Constant.level_group_100));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:7:2:100:"+hospitalEntry.getKey()).toString(),jsonObject.toString());
        }
    }

    private void computequota_8()throws Exception {
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospital2();
        Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        Map<String, Town> townsMap = new HashMap<String, Town>();
        for (Town town : towns) {
            townsMap.put(town.getCode(), town);
        }
        //找出今天的签约信息
        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterday(now,tomorrow);
        //数组里面第一个是健康人群 第二个是慢病人群 第三个是65岁以上人群
        Map<String, Map<String, Long>> cityAgeMap = new HashMap<String, Map<String, Long>>();//key是市行政代码 目前只有厦门市
        cityAgeMap.put(Constant.city,new HashMap<String, Long>());

        Map<String, Map<String, Long>> townAgeMap = new HashMap<String, Map<String, Long>>();//key是区行政代码
        Map<String, Map<String, Long>> orgAgeMap = new HashMap<String, Map<String, Long>>();//key是机构代码
        //统计有签约的
        for (SignFamily signFamily : signFamilys) {
            Hospital hospital = hospitalsMap.get(signFamily.getHospital());//得到患者签约的机构
            if(hospital==null){
                continue;
            }
            String town = hospital.getTown();
            int age = IdCardUtil.getAgeForIdcard(signFamily.getIdcard());//根据card解析年龄
            String ageCode =getAgeCode(age);//得到年龄的code
            //统计市
            if (cityAgeMap.containsKey(Constant.city)) {
                Map<String, Long> groupMapTemp = cityAgeMap.get(Constant.city);
                groupMapTemp.put(ageCode,(groupMapTemp.get(ageCode)==null?0l:groupMapTemp.get(ageCode)) + 1);
                cityAgeMap.put(Constant.city, groupMapTemp);
            } else {
                Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                groupMapTemp.put(ageCode, 1L);
                cityAgeMap.put(Constant.city, groupMapTemp);
            }
            //统计区
            if (townAgeMap.containsKey(town)) {
                Map<String, Long> groupMapTemp = townAgeMap.get(town);
                groupMapTemp.put(ageCode,(groupMapTemp.get(ageCode)==null?0l:groupMapTemp.get(ageCode)) + 1);
                townAgeMap.put(town, groupMapTemp);
            } else {
                Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                groupMapTemp.put(ageCode, 1L);
                townAgeMap.put(town, groupMapTemp);
            }
            //统计机构
            if (orgAgeMap.containsKey(hospital.getCode())) {
                Map<String, Long> groupMapTemp = orgAgeMap.get(hospital.getCode());
                groupMapTemp.put(ageCode,(groupMapTemp.get(ageCode)==null?0l:groupMapTemp.get(ageCode))+ 1);
                orgAgeMap.put(hospital.getCode(), groupMapTemp);
            } else {
                Map<String, Long> groupMapTemp = new HashMap<String, Long>();
                groupMapTemp.put(ageCode, 1L);
                orgAgeMap.put(hospital.getCode(), groupMapTemp);
            }
        }
        // 保存市的统计数据
        for (Map.Entry<String, Map<String, Long>> entry : cityAgeMap.entrySet()) {
            Map<String,Long> oneAgeMap=entry.getValue();
            //保存0~6
            JSONObject jsonObject=new JSONObject();

            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_1)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:4:1:"+entry.getKey()).toString(),jsonObject.toString());
            //保存7~18
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_2)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:4:2:"+entry.getKey()).toString(),jsonObject.toString());
            //保存18~30
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_3)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:4:3:"+entry.getKey()).toString(),jsonObject.toString());
            //保存30~50
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_4)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_4));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:4:4:"+entry.getKey()).toString(),jsonObject.toString());
            //保存50~65
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_5)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_5));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:4:5:"+entry.getKey()).toString(),jsonObject.toString());
            //保存18~30
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_6)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_6));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",Constant.cityName);
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:4:6:"+entry.getKey()).toString(),jsonObject.toString());
        }
        //保存区级
        for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
            //判断该区是否有统计数据
            Map<String, Long> oneAgeMap = townAgeMap.get(entry.getKey());//得到当个区的统计数据
            Town town = entry.getValue();//得到区级信息
            JSONObject jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_1)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:3:1:"+entry.getKey()).toString(),jsonObject.toString());
            //保存7~18
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_2)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:3:2:"+entry.getKey()).toString(),jsonObject.toString());
            //保存18~30
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_3)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:3:3:"+entry.getKey()).toString(),jsonObject.toString());
            //保存30~50
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_4)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_4));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:3:4:"+entry.getKey()).toString(),jsonObject.toString());
            //保存50~65
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_5)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_5));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:3:5:"+entry.getKey()).toString(),jsonObject.toString());
            //保存18~30
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_6)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_6));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",town.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:3:6:"+entry.getKey()).toString(),jsonObject.toString());
        }

        for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
            //判断该机构是否有统计数据
            Map<String, Long> oneAgeMap = orgAgeMap.get(entry.getKey());//得到当个机构的统计数据
            Hospital hospital = entry.getValue();//得到区级信息
            //保存0~6
            JSONObject jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_1)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_1));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:2:1:"+entry.getKey()).toString(),jsonObject.toString());
            //保存7~18
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_2)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_2));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:2:2:"+entry.getKey()).toString(),jsonObject.toString());
            //保存18~30
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_3)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_3));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:2:3:"+entry.getKey()).toString(),jsonObject.toString());
            //保存30~50
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_4)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_4));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:2:4"+entry.getKey()).toString(),jsonObject.toString());
            //保存50~65
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_5)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_5));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:2:5:"+entry.getKey()).toString(),jsonObject.toString());
            //保存18~30
            jsonObject=new JSONObject();
            if (oneAgeMap!=null&&oneAgeMap.containsKey(Constant.level_age_6)) {
                jsonObject.put("num",oneAgeMap.get(Constant.level_age_6));
            } else {
                jsonObject.put("num",0);
            }
            jsonObject.put("name",hospital.getName());
            jsonObject.put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:8:2:6:"+entry.getKey()).toString(),jsonObject.toString());
        }

    }

    private void computequota_9() {
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjQkdoctorMap = new HashMap<String, JSONObject>();//团队级的统计map 目前没有团队 先用全科医生统一  key doctorCode

        initMap(tjCityMap, tjTownMap, tjOrgMap, tjQkdoctorMap, null);
        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingWaitSignYesterday(now, tomorrow);

        //统计有签约的
        for (SignFamily signFamily : signFamilys) {
            String hospitalCode=signFamily.getHospital();
            Hospital hospital=hospitalsMap.get(hospitalCode);
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(hospital.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            //判断是中心还是服务站 服务站的话添加到中心去
            if(!"00".equals(hospital.getCode().substring(8))){
                String orgCodeTemp=hospital.getCode().substring(0,8)+"00";
                JSONObject  orgjo=tjOrgMap.get(orgCodeTemp);
                orgjo.put("num",((int)orgjo.get("num"))+1);
            }else{
                JSONObject orgjo=tjOrgMap.get(hospital.getCode());
                orgjo.put("num",((int)orgjo.get("num"))+1);
            }
            //统计全科医生
            JSONObject qkjo=tjQkdoctorMap.get(signFamily.getDoctor());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:9:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:9:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:9:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjQkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:9:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    private void computequota_10() {
        Map<String, JSONObject> tjCityMap = new HashMap<>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjTownMap = new HashMap<String, JSONObject>();//区级的统计map key 是区行政区划350200
        Map<String, JSONObject> tjOrgMap = new HashMap<String, JSONObject>();//机构的统计map key 是机构的code
        Map<String, JSONObject> tjQkdoctorMap = new HashMap<String, JSONObject>();//团队级的统计map 目前没有团队 先用全科医生统一  key doctorCode

        initMap(tjCityMap, tjTownMap, tjOrgMap, tjQkdoctorMap, null);
        String now = getDayString(0);
        String tomorrow = getDayString(1);
        List<SignFamily> signFamilys = signFamilyDao.findByJiatingChaangeSignYesterday(now, tomorrow);

        //统计有签约的
        for (SignFamily signFamily : signFamilys) {
            String hospitalCode=signFamily.getHospital();
            Hospital hospital=hospitalsMap.get(hospitalCode);
            //统计市
            JSONObject cityjo=tjCityMap.get(Constant.city);
            cityjo.put("num",((int)cityjo.get("num"))+1);
            //统计区
            JSONObject townjo=tjTownMap.get(hospital.getTown());
            townjo.put("num",((int)townjo.get("num"))+1);
            //统计机构
            JSONObject orgjo=tjOrgMap.get(hospital.getCode());
            orgjo.put("num",((int)orgjo.get("num"))+1);
            //统计全科医生
            JSONObject qkjo=tjQkdoctorMap.get(signFamily.getDoctor());
            if(qkjo!=null){
                qkjo.put("num",((int)qkjo.get("num"))+1);
            }
        }
        //统计市
        for(Map.Entry<String,JSONObject> entry:tjCityMap.entrySet()){
            entry.getValue().put("date", DateUtil.dateToStrLong(new Date()));
            redisTemplate.opsForValue().set(new StringBuffer("quota:10:4:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存区
        for(Map.Entry<String,JSONObject> entry:tjTownMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:10:3:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存机构
        for(Map.Entry<String,JSONObject> entry:tjOrgMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:10:2:"+entry.getKey()).toString(), entry.getValue().toString());
        }
        //保存全科医生
        for(Map.Entry<String,JSONObject> entry:tjQkdoctorMap.entrySet()){
            redisTemplate.opsForValue().set(new StringBuffer("quota:10:1:"+entry.getKey()).toString(), entry.getValue().toString());
        }
    }

    public Properties getSystemProperties() {
        if (systemProperties == null) {
            InputStream is = null;
            try {
                is = this.getClass().getResourceAsStream("/application.properties");
                systemProperties = new Properties();
                systemProperties.load(is);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return systemProperties;
    }

    public static String getDayString(Integer size) {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, size);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public List<Map<String,Object>> getFvYesterday() {
        String now = getDayString(0);
        String tomorrow = getDayString(1);

        String sql=" select * from fv_plan a where unix_timestamp(a.update_time)>=unix_timestamp('"+now+"') and unix_timestamp(a.update_time)< unix_timestamp('"+tomorrow+"')  ";
        return jdbcTemplate.queryForList(sql);
    }

    /**
     * 根据年龄得到对应的code
     * @param age
     * @return
     */
    public String getAgeCode(int age) {
        if (age < 7) {
            return Constant.level_age_1;
        } else if (age >= 7 && age < 18) {
            return Constant.level_age_2;
        } else if (age >= 18 && age < 30) {
            return Constant.level_age_3;
        } else if (age >= 30 && age < 50) {
            return Constant.level_age_4;
        } else if (age >= 50 && age < 65) {
            return Constant.level_age_5;
        } else {
            return Constant.level_age_6;
        }
    }

    public static void main(String[] args) {
        MyHashMap<String, String> tjCityMap = new MyHashMap<String, String>();//区级的统计map key 是区行政区划350200
        tjCityMap.put("1","1");
        tjCityMap.put("2","2");
        tjCityMap.put("4","4");
        Map<String, String> tjCityMap_1 =tjCityMap.clone();
        Map<String, String> tjCityMap_2 = tjCityMap.clone();
        tjCityMap.put("3","3");
        tjCityMap.remove("1");
        tjCityMap_1.put("33","33");
        tjCityMap_1.remove("2");
        tjCityMap_2.put("333","333");
        tjCityMap_2.remove("4");
        System.out.println(tjCityMap);
        System.out.println(tjCityMap_1);
        System.out.println(tjCityMap_2);
    }
}
